#!/home/python/.virtualenvs/open/bin/python

import sys
sys.path.append('./')
sys.path.insert(0, "../")

import os
if not os.getenv('DJANGO_SETTINGS_MODULE'):
    os.environ['DJANGO_SETTINGS_MODULE'] = 'equity_mall.settings.prod'

import django
django.setup()

from vouchers.models import AliDiscountCouponQRCode, AliDiscountCoupon
import datetime
from random import randint

from equity_mall.utils.utils import random_str


def generate_qrcode():
    '''生产活动有效链接'''
    alidiscountcoupon = AliDiscountCoupon.objects.get(id=87)
    alidiscountcoupon_level = alidiscountcoupon.alidiscountcouponlevel_set.all().first()
    count = alidiscountcoupon_level.alidiscountcouponqrcode_set.all().count()
    range_count = alidiscountcoupon.max_number - count
    for i in range(10):
        discountcouponqrcodedata = {}
        identify = random_str()
        common_url = f'https://api.sutpay.com/customer/applet/?identify={identify}'
        discountcouponqrcodedata['identify'] = identify
        discountcouponqrcodedata['common_url'] = common_url
        out_biz_no = f'100003{str(datetime.datetime.now()).replace("-", "").replace(":", "").replace(" ", "").replace(".", "")}{"%06d" % randint(0, 999999)}'
        discountcouponqrcodedata['alidiscountcoupon_level_id'] = alidiscountcoupon_level.id
        discountcouponqrcodedata['out_biz_no'] = out_biz_no
        discountcouponqrcodedata, create = AliDiscountCouponQRCode.objects.get_or_create(defaults=discountcouponqrcodedata, identify=identify)


if __name__ == '__main__':
    generate_qrcode()